'use strict';

const await = require('await-stream-ready/lib/await');
const BaseController = require('./base');
/**
 * @Controller 收藏模块
 */
class CollectController extends BaseController {
  /**
   * @Router post /api/collect/create
   * @Request body creteCollectRequest
   * @Summary 新增收藏
   */
  async create() {
    const { ctx, app, service } = this;
    const body = ctx.request.body;
    // 判断是否重复收藏
    const isExist = await service.collect.info(body)
    if (isExist) {
      this.Error({ message: '收藏失败,当前内容已被收藏' });
      return
    }
    body.collectId = Date.now();
    const result = await service.collect.create(body);
    if (result) {
      this.Success({ message: '收藏成功' });
    } else {
      this.Error({ message: '收藏失败' });
    }
  }


  /**
   * @Router post /api/collect/list
   * @Request body listCollectRequest
   * @Summary 获取指定类型的所有收藏
   */
  async list() {
    const { ctx, service } = this;
    let body = ctx.request.body;
    const data = await service.collect.list(body);
    if (data) {
      this.Success({ data, message: '获取成功' });
    } else {
      this.Error({ message: '获取失败' });
    }
  }

  /**
   * @Router post /api/collect/info
   * @Request body infoCollectRequest
   * @Summary 获取收藏详情信息
   */
  async info() {
    const { ctx, service } = this;
    let body = ctx.request.body;
    const data = await service.collect.info(body);
    if (data) {
      this.Success({ data, message: '获取成功' });
    } else {
      this.Error({ message: '获取失败,收藏不存在' });
    }
  }

  /**
   * @Router post /api/collect/edit
   * @Request body editCollectRequest
   * @Summary 修改收藏信息
   */
  async edit() {
    const { ctx, service } = this;
    let body = ctx.request.body;
    const data = await service.collect.edit(body);
    if (data) {
      this.Success({ message: '修改成功' });
    } else {
      this.Error({ message: '修改失败' });
    }
  }
  

    /**
   * @Router post /api/collect/cancel
   * @Request body cancelCollectRequest
   * @Summary 取消收藏信息
   */
  async cancel() {
    const { ctx, service } = this;
    let body = ctx.request.body;
    const data = await service.collect.cancel(body);
    if (data) {
      this.Success({ message: '取消收藏成功' });
    } else {
      this.Error({ message: '取消收藏失败' });
    }
  }

}

module.exports = CollectController;
